<template>
  <div class="container">
    <div v-if="type === 'edit'" class="detail">
      <div class="header">
        <span>编辑</span>
        <div class="btn-group">
          <el-button class="query" @click="changeStatus">确定</el-button>
          <el-button plain class="clear" @click="$router.back()">取消</el-button>
        </div>
      </div>
    </div>
    <div class="detail">
      <div class="title">
        <div class="icon"></div>
        <div class="desc">店铺信息</div>
      </div>
      <el-form ref="form" :model="form" label-position="top">
        <el-row type="flex" justify="start" :gutter="20">
          <el-col :span="6">
            <el-form-item prop="shopType">
              <template #label>
                <div>店铺名称<span style="color: red">*</span></div>
              </template>
              <el-select v-model="form.shopName" :disabled="readonly" placeholder="请选择店铺名称" style="width: 100%" @change="changeShopName">
                <el-option
                  v-for="item in shopListFilter"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="shopName">
              <template #label>
                <div>商店类型<span style="color: red">*</span></div>
              </template>
              <el-select v-model="form.shopType" :disabled="readonly" placeholder="请选择商店类型" clearable style="width: 100%" @change="changeShopType">
                <el-option
                  v-for="item in shopTypeList"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="shopAddress">
              <template #label>
                <div>商店地址<span style="color: red">*</span></div>
              </template>
              <el-input
                readonly
                v-model="form.shopAddress"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="jingquName">
              <template #label>
                <div>所属景区<span style="color: red">*</span></div>
              </template>
              <el-input
                readonly
                v-model="form.jingquName"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="title">
        <div class="icon"></div>
        <div class="desc">状态<span style="color: red">*</span></div>
      </div>
      <div class="status">
        <div class="choose_item" @click="chooseShopStatus('0')">
          <img v-if="shopStatus === '0'" src="@/assets/images/selected_circle.png" alt="">
          <img v-else src="@/assets/images/un_selected_circle.png" alt="">
          <span>正常营业</span>
        </div>
        <div class="choose_item" @click="chooseShopStatus('1')">
          <img v-if="shopStatus === '1'" src="@/assets/images/selected_circle.png" alt="">
          <img v-else src="@/assets/images/un_selected_circle.png" alt="">
          <span>暂停营业</span>
        </div>
        <div class="choose_item" @click="chooseShopStatus('2')">
          <img v-if="shopStatus === '2'" src="@/assets/images/selected_circle.png" alt="">
          <img v-else src="@/assets/images/un_selected_circle.png" alt="">
          <span>店铺撤场</span>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="table-header">
        <span class="left">列表</span>
        <div class="right">
          <el-button type="primary" icon="el-icon-plus" class="add" @click="toMerchantAdd">新增</el-button>
          <el-button type="primary" icon="el-icon-download" class="export" @click="exportHandle">导出</el-button>
          <el-button icon="el-icon-refresh-right" class="refresh" @click="refresh">刷新</el-button>
        </div>
      </div>
      <div class="table-box">
        <el-table
          :data="merchandiseList"
          :header-cell-style="headerCellStyle"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
            width="80"
            align="center">
          </el-table-column>
          <el-table-column
            prop="merchandiseName"
            label="商品名称"
            min-width="350"
            align="center">
          </el-table-column>
          <el-table-column
            prop="merchandisePrice"
            label="商品价格">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            min-width="120"
            fixed="right">
            <template slot-scope="scope">
              <el-link type="primary" class="look" @click="look(scope.row.id)">查看</el-link>
              <el-link type="success" class="edit" @click="edit(scope.row.id)">编辑</el-link>
              <el-link type="warning" class="delete" @click="remove(scope.row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <span>总共 {{pageTotal}} 条</span>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            @prev-click="handlePrevClick"
            @next-click="handleNextClick"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="prev, pager, next, sizes"
            :total="pageTotal">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {getDicts} from "@/api/system/dict/data";
import {
  getCulturalAndCreativeList,
  getMerchandiseList,
  merchandiseDetele,
  merchandiseListExport
} from "@/api/form/cultrue";
import {qiyeEdit} from "@/api/form/enterprise";
import {downloadFile} from "@/utils";

export default {
  mounted() {
    const { type, shopId } = this.$route.query
    console.warn(this.$route.query)
    this.type = type
    this.shopId = shopId
    if (this.shopId) {
      this.getMerchandiseList(this.shopId)
    }
    this.getCulturalAndCreativeList()
    this.getQiyeType()
  },
  data() {
    return {
      form: {
        shopName: '',
        shopType: '',
        shopAddress: '',
        jingquName: ''
      },
      type: '', // edit look
      shopId: '',
      shopTypeList: [],
      shopList: [],
      shopListFilter: [],
      shopStatus: '',

      merchandiseList: [],
      currentPage: 1,
      pageSize: 10,
      pageTotal: 0
    }
  },
  computed: {
    headerCellStyle () {
      return {'font-size': '16px','font-weight': 400,color: '#999999',background: '#fff'}
    },
    readonly() {
      return this.type !== 'add'
    }
  },
  methods: {
    async exportHandle() {
      const { code, data, msg } = await merchandiseListExport({
        qiYeId: this.shopId,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      })
      if (code !== 200) {
        this.$message({ message: msg, type: 'error' })
        return
      }
      downloadFile(data, '商品列表')
      this.$message({ message: '已导出', type: 'success' })
    },
    look(id) {
      this.$router.push(`/form/culture/merchant/detail?type=look&id=${id}&shopId=${this.shopId}`)
    },
    edit(id) {
      this.$router.push(`/form/culture/merchant/edit?type=edit&id=${id}&shopId=${this.shopId}`)
    },
    async remove(id) {
      const {code, msg} = await merchandiseDetele({
        id
      })
      if (code !== 200) {
        this.$message({ message: msg, type: 'error' })
      }
      this.$message({ message: '已删除', type: 'success' })
      await this.getMerchandiseList(this.shopId)
    },
    async changeStatus() {
      const { code, msg } = await qiyeEdit({
        id: this.shopId,
        status: this.shopStatus
      })
      if (code !== 200) {
        this.$message({message: msg, type: 'error'})
        return
      }
      this.$message({message: '已修改', type: 'success'})
    },
    refresh() {
      if (!this.shopId) return
      this.getMerchandiseList(this.shopId)
    },
    async getQiyeType() {
      const { code, data } = await getDicts('merchant_type')
      if (code === 200) {
        this.shopTypeList = data
      }
    },
    async getCulturalAndCreativeList() {
      const { code, data, msg } = await getCulturalAndCreativeList()
      if (code === 200) {
        this.shopList = data.rows
        if (this.type !== 'add') {
          console.warn(this.shopList, this.shopId)
          const shop = this.shopList.find(item => item.id == this.shopId)
          this.form.shopName = shop.name
          this.form.shopType = shop.type
          this.form.shopAddress = shop.address
          this.form.jingquName = shop.jingquName
          this.shopStatus = shop.status
        }
        this.shopListFilter = data.rows
      } else {
        this.$message({message: msg, type: 'error'})
      }
    },
    async getMerchandiseList(shopId) {
      const { code, data, msg } = await getMerchandiseList({
        qiYeId: shopId,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      })
      if (code === 200) {
        this.merchandiseList = data.rows
        this.pageTotal = data.total
      } else {
        this.$message({message: msg, type: 'error'})
      }
    },
    getShopTypeName(type) {
      const dict = this.shopTypeList.find(item => item.dictValue === type)
      if (!dict) return '-'
      return dict.dictLabel
    },
    changeShopName(shopId) {
      const shop = this.shopList.find(item => item.id === shopId)
      this.form.shopType = shop.type
      this.form.shopAddress = shop.address
      this.form.jingquName = shop.jingquName
      this.shopStatus = shop.status
      this.shopId = shopId
      this.getMerchandiseList(shopId)
    },
    changeShopType(dictValue) {
      this.form = { shopType: dictValue }
      if (!dictValue) {
        this.shopListFilter = this.shopList
        return
      }
      // 根据类型筛选名称列表
      this.shopListFilter = this.shopList.filter(item => item.type === dictValue)
    },
    chooseShopStatus(status) {
      if (this.type !== 'edit') return
      this.shopStatus = status
    },
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.getMerchandiseList(this.shopId)
    },
    handleCurrentChange (currentPage) {
      this.currentPage = currentPage
      this.getMerchandiseList(this.shopId)
    },
    handlePrevClick (currentPage) {
      this.currentPage = currentPage
      this.getMerchandiseList(this.shopId)
    },
    handleNextClick (currentPage) {
      this.currentPage = currentPage
      this.getMerchandiseList(this.shopId)
    },
    toMerchantAdd() {
      if (!this.shopId) return
      this.$router.push('/form/culture/merchant?shopId=' + this.shopId)
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  background: var(--theme_bg_color);
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-family: HarmonyOS Sans SC, HarmonyOS Sans SC;
      font-weight: 400;
      font-size: 16px;
      color: #333333;
      line-height: 19px;
      text-align: left;
      font-style: normal;
      text-transform: none;
    }
    .btn-group {
      display: flex;
      align-items: center;
      justify-content: flex-end;

      .query {
        width: 72px;
        height: 32px;
        background: #3370FF;
        border: none;
        color: #ffffff;
      }

      .clear {
        width: 72px;
        height: 32px;
      }
    }
  }
  .detail {
    width: 100%;
    background: #FFFFFF;
    margin: 16px 0;
    padding: 16px;
    box-sizing: border-box;
    .title {
      display: flex;
      align-items: center;
      margin-bottom: 25px;
      .icon {
        width: 4px;
        height: 16px;
        background: #3370FF;
        margin-right: 10px;
      }
      .desc {
        font-size: 16px;
        font-weight: 400;
        color: #333333;
      }
    }
    .form {
      width: 100%;
      display: flex;
    }
    .status {
      display: flex;
      align-items: center;
      .choose_item {
        margin-right: 30px;
        display: flex;
        align-items: center;
        cursor: pointer;
        img {
          width: 16px;
          height: 16px;
          margin-right: 10px;
        }
        span {
          font-size: 14px;
          font-weight: 400;
          color: #333333;
        }
      }
    }
  }
  .content {
    width: 100%;
    background: #f2f3f5;
    .table-header {
      padding: 10px 15px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #E4E7ED;
      background-color: #fff;
      .left {
        font-size: 16px;
        font-weight: 400;
        color: #111111;
      }
      .right {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        .add {
          background: #3370FF;
          border: none;
        }
        .export {
          background: #79CF1C;
          border: none;
        }
      }
    }
    .table-box {
      height: 100%;
      position: relative;
    }
    .status-normal {
      color: #2E69F8;
    }
    .status-stop {
      color: #FF8521;
    }
    .status-clear {
      color: #999999;
    }
    .pagination {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      padding: 10px 0;
      background: #fff;
      span {
        font-size: 5rpx;
      }
    }
    .edit {
      margin: 0 20px;
    }
    .tag {
      width: 60px;
      height: 24px;
      border-radius: 2px 2px 2px 2px;
      font-size: 14px;
      font-weight: 400;
      display: flex;
      align-items: center;
      justify-content: center;
      i {
        margin-right: 5px;
      }
    }
    .business {
      background: #D6E2FF;
      color: #245BDB;
    }
  }
}
</style>
